<template>
    <el-dialog ref="dialogform" :title="addEditName + title" :visible.sync="dialogVisible" width="50%"
        :before-close="handleClose">
        <template v-if="addEditFlag == 'edit'">
            <span class="msgInfo successText" v-if="ruleForm.status == 'COMPLETED'">该批次已完成</span>
            <span class="msgInfo errorText" v-else-if="ruleForm.status !== 'CREATE'">该批次已进入养殖阶段不可编辑</span>
        </template>
        <el-form :disabled="ruleForm.status !== 'CREATE' && addEditFlag !== 'add'" v-if="addEditFlag !== 'bindFishPond'"
            :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px" class="demo-ruleForm">
            <!-- <el-form-item label="批次编号" prop="batchName">
                <el-input v-model="ruleForm.batchName" class="w100p" clearable placeholder="请输入批次编号"></el-input>
            </el-form-item> -->
            <el-form-item label="批次编号" prop="batchCode">
                <el-input v-model="ruleForm.batchCode" class="w100p" clearable placeholder="请输入批次编号"></el-input>
            </el-form-item>
            <el-form-item v-show="addEditFlag == 'add'" label="关联鱼塘" prop="fishPondId">
                <el-select v-model="ruleForm.fishPondId" filterable clearable class="w100p">
                    <el-option v-for="(item, index) in fishPondList" :key="index" :label="item.pondName"
                        :value="item.fishPondId" />
                </el-select>
            </el-form-item>
            <el-form-item label="供应商" prop="supplierBatchCode">
                <el-input v-model="ruleForm.supplierBatchCode" class="w100p" clearable
                    placeholder="请输入供应商名称"></el-input>
            </el-form-item>
            <el-form-item label="养殖类型" prop="breedType">
                <el-select v-model="ruleForm.breedType" filterable clearable class="w100p">
                    <el-option v-for="(item, index) in breedTypeList" :key="index" :label="item.breedTypeName"
                        :value="item.breedTypeNo" />
                </el-select>
            </el-form-item>
            <el-form-item label="创建时间" prop="buildDate">
                <el-date-picker v-model="ruleForm.buildDate" value-format="yyyy-MM-dd HH:mm:ss" type="datetime"
                    placeholder="选择日期时间" :picker-options="pickerOptionsBuildDateTime" class="w100p">
                </el-date-picker>
            </el-form-item>
            <el-form-item label="失效日期">
                <el-date-picker v-model="ruleForm.expirationDate" value-format="yyyy-MM-dd" type="date"
                    placeholder="选择日期时间" :picker-options="pickerOptionsExpirationDate" class="w100p">
                </el-date-picker>
            </el-form-item>
        </el-form>
        <el-form :disabled="ruleForm.status !== 'CREATE' && addEditFlag !== 'add'" v-if="addEditFlag == 'bindFishPond'"
            :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px" class="demo-ruleForm">
            <el-form-item label="批次编号" prop="batchName">
                <el-input disabled v-model="ruleForm.batchName" class="w100p" clearable
                    placeholder="请输入批次编号"></el-input>
            </el-form-item>
            <el-form-item label="批次编号" prop="batchCode">
                <el-input disabled v-model="ruleForm.batchCode" class="w100p" clearable
                    placeholder="请输入批次编号"></el-input>
            </el-form-item>
            <el-form-item label="关联鱼塘" prop="fishPondId">
                <el-select v-model="ruleForm.fishPondId" filterable clearable class="w100p">
                    <el-option v-for="(item, index) in fishPondList" :key="index" :label="item.pondName"
                        :value="item.fishPondId" />
                </el-select>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="cancel">取 消</el-button>
            <SaveDraftBox ref="saveDraft" v-if="addEditFlag == 'add' && dialogVisible" :formData="ruleForm"
                @pushData="pushData">
            </SaveDraftBox>
            <el-button type="primary" @click="submitForm">确 定</el-button>
        </span>
    </el-dialog>
</template>
<script>
import batchManagement from '@/api/production-management/batch-management.js';
export default {
    components: {},
    props: {
        addEditFlag: {
            type: String,
            default: () => {
                return 'add'
            }
        },
        data: {
            type: Object,
            default: () => {
                return {}
            }
        },
        title: {
            type: String,
            default: () => {
                return '操作表单'
            }
        },
        dialogVisible: {
            type: Boolean,
            default: () => {
                return false
            }
        },
        fishPondList: {
            type: Array,
            default: () => {
                return []
            }
        },
        breedTypeList: {
            type: Array,
            default: () => {
                return []
            }
        },
    },
    data() {
        return {
            pickerOptionsBuildDateTime: {
                disabledDate(time) {
                    return time.getTime() >= Date.now();
                },
                shortcuts: [{
                    text: '今天',
                    onClick(picker) {
                        picker.$emit('pick', new Date());
                    }
                }, {
                    text: '昨天',
                    onClick(picker) {
                        const date = new Date();
                        date.setTime(date.getTime() - 3600 * 1000 * 24);
                        picker.$emit('pick', date);
                    }
                }, {
                    text: '一周前',
                    onClick(picker) {
                        const date = new Date();
                        date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
                        picker.$emit('pick', date);
                    }
                }]
            },
            pickerOptionsExpirationDate: {
                disabledDate(time) {
                    return time.getTime() < Date.now();
                },
                shortcuts: [{
                    text: '明天',
                    onClick(picker) {
                        const date = new Date();
                        date.setTime(date.getTime() + 3600 * 1000 * 24);
                        picker.$emit('pick', date);
                    }
                }, {
                    text: '后天',
                    onClick(picker) {
                        const date = new Date();
                        date.setTime(date.getTime() + 3600 * 1000 * 24 * 2);
                        picker.$emit('pick', date);
                    }
                }, {
                    text: '一周后',
                    onClick(picker) {
                        const date = new Date();
                        date.setTime(date.getTime() + 3600 * 1000 * 24 * 7);
                        picker.$emit('pick', date);
                    }
                }]
            },
            addEditName: '添加',
            addEditApi: '',
            ruleForm: {},
            rules: {
                batchName: [
                    { required: true, message: '请输入批次编号', trigger: 'change' }
                ],
                batchCode: [
                    { required: true, message: '请输入批次号', trigger: 'change' }
                ],
                breedType: [
                    { required: true, message: '请选择类型', trigger: 'change' }
                ],
                buildDate: [
                    { required: true, message: '请选择创建时间', trigger: 'change' }
                ],
                expirationDate: [
                    { required: true, message: '请选择失效日期', trigger: 'change' }
                ],
                // fishPondId: [
                //     { required: true, message: '请选择鱼塘', trigger: 'change' }
                // ],
            }
        }
    },
    computed: {

    },
    watch: {
        dialogVisible(v) {
            v && this.initData()
        }
    },

    mounted() { },
    methods: {
        initData() {
            if (this.addEditFlag == 'add') {
                this.resetForm()
                this.addEditApi = batchManagement.add
                this.addEditName = '添加';
                this.getInitRulFormData();
            } else if (this.addEditFlag == 'edit') {
                this.ruleForm = { ...this.data }
                this.addEditApi = batchManagement.edit
                this.addEditName = '编辑'
            } else if (this.addEditFlag == 'bindFishPond') {
                this.ruleForm = { ...this.data }
                this.addEditApi = batchManagement.bindFishPond
                this.addEditName = '绑定鱼塘'
            }
        },
        pushData(data) {
            this.ruleForm = { ...data };
            this.$nextTick(() => {
                this.$refs.ruleForm.clearValidate();
            })
        },
        //初始化一些表单数据
        getInitRulFormData() {
            let params = {
                buildDate: this.$formatTime(new Date(), 'yyyy-MM-dd HH:mm:ss')
            }
            this.$set(this, 'ruleForm', params);
        },
        //提交表单
        submitForm() {
            this.$refs.ruleForm.validate((valid) => {
                if (valid) {
                    let params = {};
                    if (this.addEditFlag == 'bindFishPond') {
                        params = {
                            fishPondId: this.ruleForm.fishPondId,
                            batchId: this.ruleForm.batchId
                        };
                    } else {
                        params = { ...this.ruleForm };
                    }
                    this.addEditApi({ ...params, batchName: params.batchCode }).then(res => {
                        let msg = res.success ? '操作成功' : '操作失败'
                        this.$message[res.success ? 'success' : 'warning'](res.message || msg)
                        res.success && this.cancel();
                        //提交数据时删除保存的记录
                        res.success && this.addEditFlag == 'add' && this.$refs.saveDraft.delByKey();
                    }).catch(err => {
                        this.$message.error(err)
                    })
                } else {
                    return false;
                }
            })
        },
        //重置
        resetForm() {
            this.ruleForm = {};
            this.$refs.ruleForm && this.$refs.ruleForm.resetFields()
        },
        //取消关闭
        cancel() {
            this.resetForm()
            this.$emit("closeDialog")
        },
        handleClose(done) {
            this.$confirm('确认关闭？')
                .then(_ => { this.resetForm(); this.cancel() })
                .catch(_ => { });
        }
    }
};
</script>
<style lang="scss" scoped>
.msgInfo {
    padding-left: 60px;
    text-align: right;
    display: inline-block;
    margin-bottom: 15px;
}
</style>